package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购单位验收合同Controller
 * 
 * @author ruoyi
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/acceptancecontract")
public class SysAcceptancecontractController extends BaseController
{
    private String prefix = "system/acceptancecontract";

    @Autowired
    private ISysAcceptancecontractService sysAcceptancecontractService;
    @Autowired
    private ISysProcurementdeclareService sysProcurementdeclareService;
    @Autowired
    private ISysPurchasingunitService sysPurchasingunitService;
    @Autowired
    private ISysProvidecontractService sysProvidecontractService;
    @Autowired
    private ISysSupplierService sysSupplierService;
    @Autowired
    private ISysProvidecontractdetailsService sysProvidecontractdetailsService;
    @Autowired
    private ISysAcceptancegoodsService sysAcceptancegoodsService;
    @Autowired
    private ISysEvaluateService sysEvaluateService;
    @Autowired
    private ISysEvaluateaverageService sysEvaluateaverageService;

    @RequiresPermissions("system:acceptancecontract:view")
    @GetMapping()
    public String acceptancecontract()
    {
        return prefix + "/acceptancecontract";
    }

    /**
     * 查询采购单位验收合同列表
     */
        @RequiresPermissions("system:acceptancecontract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysAcceptancecontract sysAcceptancecontract)
    {

        SysUser sysUser = ShiroUtils.getSysUser();
        List<SysAcceptancecontract> list = new ArrayList<SysAcceptancecontract>();
        if(sysUser.getUserType().equals("00")){
            startPage();
            list = sysAcceptancecontractService.selectSysAcceptancecontractList(sysAcceptancecontract);
        }else{
            sysAcceptancecontract.setPurchasingUnitId(sysUser.getCompanyId());
            startPage();
            list = sysAcceptancecontractService.selectSysAcceptancecontractList(sysAcceptancecontract);
        }

        return getDataTable(list);
    }

    /**
     * 导出采购单位验收合同列表
     */
    @RequiresPermissions("system:acceptancecontract:export")
    @Log(title = "采购单位验收合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAcceptancecontract sysAcceptancecontract)
    {
        List<SysAcceptancecontract> list = sysAcceptancecontractService.selectSysAcceptancecontractList(sysAcceptancecontract);
        ExcelUtil<SysAcceptancecontract> util = new ExcelUtil<SysAcceptancecontract>(SysAcceptancecontract.class);
        return util.exportExcel(list, "acceptancecontract");
    }

    /**
     * 新增采购单位验收合同
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        SysPurchasingunit sysPurchasingunit = sysPurchasingunitService.selectSysPurchasingunitById(sysUser.getCompanyId());
        SysProcurementdeclare sysProcurementdeclare = new SysProcurementdeclare();//采购申报实体对象
        sysProcurementdeclare.setPurchasingUnitId(sysUser.getCompanyId());
        List<SysProcurementdeclare> sysProcurementdeclareList = sysProcurementdeclareService.selectSysProcurementdeclareList(sysProcurementdeclare);
        SysProvidecontract sysProvidecontract = new SysProvidecontract();//供货合同实体对象
        sysProvidecontract.setPurchasingUnitId(sysUser.getCompanyId());
        List<SysProvidecontract > sysProvidecontractList = sysProvidecontractService.selectSysProvidecontractList(sysProvidecontract);
        mmap.put("sysProcurementdeclareList",sysProcurementdeclareList);
        mmap.put("sysProvidecontractList",sysProvidecontractList);
        return prefix + "/add";
    }

    /**
     * 新增保存采购单位验收合同
     */
    @RequiresPermissions("system:acceptancecontract:add")
    @Log(title = "采购单位验收合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAcceptancecontract sysAcceptancecontract)
    {
        SysProcurementdeclare sysProcurementdeclare = sysProcurementdeclareService.selectSysProcurementdeclareById(sysAcceptancecontract.getProcurementDeclareId());
        SysProvidecontract sysProvidecontract = sysProvidecontractService.selectSysProvidecontractById(sysAcceptancecontract.getProvideContractId());
        int jie=0;
        String pingjia = sysAcceptancecontract.getAttribute3();
        String pingfen = sysAcceptancecontract.getAttribute1();
        String pingfen2 = sysAcceptancecontract.getAttribute2();
        if(sysProcurementdeclare != null && sysProvidecontract != null){
            sysAcceptancecontract.setPurchasingUnitId(sysProcurementdeclare.getPurchasingUnitId());
            sysAcceptancecontract.setUnitName(sysProcurementdeclare.getUnitName());
            sysAcceptancecontract.setUnitNumber(sysProcurementdeclare.getProjectNumber());
            sysAcceptancecontract.setExpectMoney(sysProcurementdeclare.getExpectMoney());
            sysAcceptancecontract.setUnitManager(sysProcurementdeclare.getContactName());
            sysAcceptancecontract.setUnitTel(sysProcurementdeclare.getTel());

            SysSupplier sysSupplier = sysSupplierService.selectSysSupplierById(sysProvidecontract.getSupplierId());
            sysAcceptancecontract.setManager(sysSupplier.getSupplierPerson());
            sysAcceptancecontract.setSupplierPhone(sysSupplier.getTelephone());
            sysAcceptancecontract.setSupplierName(sysSupplier.getSupplierName());
            sysAcceptancecontract.setAcceptanceResult("验收合格");
            SysProvidecontractdetails sysProvidecontractdetails = new SysProvidecontractdetails();
            sysProvidecontractdetails.setProvideContractId(sysProvidecontract.getProvideContractId());
            List<SysProvidecontractdetails> sysProvidecontractdetailsList = sysProvidecontractdetailsService.selectSysProvidecontractdetailsList(sysProvidecontractdetails);
            double zuizhong =0;
            for(SysProvidecontractdetails providecontractdetails:sysProvidecontractdetailsList){
              double price =  providecontractdetails.getNumber()*providecontractdetails.getDealPrice();
                zuizhong += price;
                }
            sysAcceptancecontract.setDealPrice(zuizhong);
            jie = sysAcceptancecontractService.insertSysAcceptancecontract(sysAcceptancecontract);
            //以上是验收单信息
            /**
             * 以下是验收单货物信息
             */
            SysAcceptancegoods acceptancegoods = new SysAcceptancegoods();
            String goodName = "";
            for(SysProvidecontractdetails providecontractdetails2:sysProvidecontractdetailsList){
                acceptancegoods.setAcceptanceContractId(sysAcceptancecontract.getAcceptanceContractId());
                acceptancegoods.setItems(providecontractdetails2.getGoodName());
                acceptancegoods.setModels(providecontractdetails2.getModel());
                acceptancegoods.setPrice(providecontractdetails2.getDealPrice());
                acceptancegoods.setNumber(providecontractdetails2.getNumber());
                acceptancegoods.setAmount(providecontractdetails2.getNumber()*providecontractdetails2.getDealPrice());
                acceptancegoods.setEquality(pingjia);
                if(goodName == ""){
                    goodName = providecontractdetails2.getGoodName();
                }else{
                    goodName += ","+ providecontractdetails2.getGoodName();
                }
                sysAcceptancegoodsService.insertSysAcceptancegoods(acceptancegoods);
            }
            /**
             * 以下是评价表信息
             */
            SysEvaluate sysEvaluate = new SysEvaluate();
            sysEvaluate.setAcceptanceContractId(sysAcceptancecontract.getAcceptanceContractId());
            sysEvaluate.setSupplierId(sysSupplier.getSupplierId());
            sysEvaluate.setSupplierName(sysSupplier.getSupplierName());
            sysEvaluate.setUnitName(sysProcurementdeclare.getUnitName());
            sysEvaluate.setGoodName(goodName);
            sysEvaluate.setQualityScore(pingfen);
            sysEvaluate.setServiceScore(pingfen2);
            sysEvaluate.setComment(pingjia);
            sysEvaluateService.insertSysEvaluate(sysEvaluate);
            /**
             * 以下是评价评分信息
             */
            SysEvaluateaverage sysEvaluateaverage = new SysEvaluateaverage();
            sysEvaluateaverage.setSupplierId(sysSupplier.getSupplierId());
            sysEvaluateaverage.setSupplierName(sysSupplier.getSupplierName());
            sysEvaluateaverage.setQualityAverage(pingfen);//质量平均分
            sysEvaluateaverage.setQualityMeaning(pingfen);//质量总分
            sysEvaluateaverage.setServiceAverage(pingfen2);//服务平均分
            sysEvaluateaverage.setServiceMeaning(pingfen2);//服务总分
            sysEvaluateaverage.setAttribute6(sysAcceptancecontract.getAcceptanceContractId()+"");
            sysEvaluateaverageService.insertSysEvaluateaverage(sysEvaluateaverage);
        }
        return toAjax(jie);
    }

    /**
     * 修改采购单位验收合同
     */
    @GetMapping("/edit/{acceptanceContractId}")
    public String edit(@PathVariable("acceptanceContractId") Long acceptanceContractId, ModelMap mmap)
    {
        SysAcceptancecontract sysAcceptancecontract = sysAcceptancecontractService.selectSysAcceptancecontractById(acceptanceContractId);
        mmap.put("sysAcceptancecontract", sysAcceptancecontract);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购单位验收合同
     */
    @RequiresPermissions("system:acceptancecontract:edit")
    @Log(title = "采购单位验收合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAcceptancecontract sysAcceptancecontract)
    {
        return toAjax(sysAcceptancecontractService.updateSysAcceptancecontract(sysAcceptancecontract));
    }

    /**
     * 删除采购单位验收合同
     */
    @RequiresPermissions("system:acceptancecontract:remove")
    @Log(title = "采购单位验收合同", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String [] ides = ids.split(",");
        for (int i = 0;i <ides.length;i++){
            SysAcceptancegoods acceptancegoods = new SysAcceptancegoods();
            acceptancegoods.setAcceptanceContractId(Long.parseLong(ides[i]));
            List<SysAcceptancegoods> acceptancegoodsList = sysAcceptancegoodsService.selectSysAcceptancegoodsList(acceptancegoods);
           for (SysAcceptancegoods sysAcceptancegoods:acceptancegoodsList){
               sysAcceptancegoodsService.deleteSysAcceptancegoodsById(sysAcceptancegoods.getAcceptanceGoodsId());
           }
        }
        return toAjax(sysAcceptancecontractService.deleteSysAcceptancecontractByIds(ids));
    }
    /**
     * 验收单商品详细
     */

    @GetMapping("/detail/{acceptanceContractId}")
    public String detail(@PathVariable("acceptanceContractId") Long acceptanceContractId, ModelMap mmap)
    {
        mmap.put("sysAcceptancecontract", sysAcceptancecontractService.selectSysAcceptancecontractById(acceptanceContractId));
        mmap.put("sysAcceptancecontracts", sysAcceptancecontractService.selectSysAcceptancecontractList(null));
        return "system/acceptancegoods/acceptancegoods";
    }

    /**
     * 验收单预览
     * @param acceptanceContractId
     * @param mmap
     * @return
     */
    @GetMapping("/yulan/{acceptanceContractId}")
    public String yulan(@PathVariable("acceptanceContractId") Long acceptanceContractId, ModelMap mmap){
        SysAcceptancecontract sysAcceptancecontract = sysAcceptancecontractService.selectSysAcceptancecontractById(acceptanceContractId);
        String telPhone = sysAcceptancecontract.getUnitTel();
        if(telPhone.length()>=7){
            String phoneNumber = telPhone.substring(telPhone.length()-7, telPhone.length());
            sysAcceptancecontract.setUnitTel(phoneNumber);
        }else{
            sysAcceptancecontract.setUnitTel(telPhone);
        }
        SysEvaluate sysEvaluate = new SysEvaluate();
        sysEvaluate.setAcceptanceContractId(acceptanceContractId);
        List<SysEvaluate> sysEvaluateList = sysEvaluateService.selectSysEvaluateList(sysEvaluate);


        SysAcceptancegoods sysAcceptancegoods = new SysAcceptancegoods();
        sysAcceptancegoods.setAcceptanceContractId(acceptanceContractId);
        List<SysAcceptancegoods> acceptancegoodsList = sysAcceptancegoodsService.selectSysAcceptancegoodsList(sysAcceptancegoods);
        mmap.put("acceptanceContract",sysAcceptancecontract);
        mmap.put("acceptanceGoods",acceptancegoodsList);
        mmap.put("changdu",acceptancegoodsList.size());
        mmap.put("evaluate",sysEvaluateList);

        return prefix + "/acceptanceYuLan";
    }
}
